
package gui.properties;

import gui.properties.groups.AbstractGroup;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Observable;
import java.util.Observer;
import java.util.Scanner;

import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import util.reflection.MethodAction;
import util.reflection.Reflection;

import controller.AbstractController;
import controller.PropertiesController;
import controller.SLogoController;

import model.Model;
import model.Turtle;

/**
 * @author Troy Ferrell
 *
 * @author Shun Fan
 * 
 */

public class TurtlePropertiesPanel extends JPanel implements Observer
{
	private JComboBox myTurtlesCombo;
	private JComboBox myTrailStroke;
	private JComboBox myVisibility;
	private JComboBox myShape;
	private JSlider myTrailWidthSlider;
	private JColorChooser myPenColorChooser;
	private JLabel myXLabel;
	private JLabel myYLabel;
	private JLabel myDirectionLabel;
	private ActionListener listener;
	
	ArrayList<AbstractGroup> linkedObjects = new ArrayList<AbstractGroup>();	
	
	private Model myModel;
	private PropertiesController myController;
	
	private String selectedTurtle = "";
	private static final String SCANNER_DELIMITTER = ";\\s*";
	
	public TurtlePropertiesPanel(String resourceFile, Model model, PropertiesController controller) {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		myModel = model;
		myModel.addObserver(myModel.TURTLE_OBSERVER, this);
		myController = controller;
		
		createTurtleChooser();
		//createWidthSlider();
		
		try {
			Scanner reader = new Scanner(new File(resourceFile)).useDelimiter(SCANNER_DELIMITTER);
			while(reader.hasNext()) {
				AbstractGroup p = (AbstractGroup) Reflection.createInstance(reader.next(), myModel, myController, reader.nextLine()) ;
				linkedObjects.add(p);
				add(p);
				//System.out.print(reader.next());
				//System.out.print(" *");
				//System.out.print(reader.nextLine());
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}	
		
		update(null, null);
	}

	/*
	private void createWidthSlider() {
		JPanel panel = new JPanel();		

		int slider_min = 0;
		int slider_max = 10;
		
		myTrailWidthSlider = new JSlider(JSlider.HORIZONTAL, slider_min, slider_max, slider_max/2);
		myTrailWidthSlider.setMajorTickSpacing(10);
		Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
		labelTable.put( (Integer)slider_min, new JLabel( ((Integer)slider_min).toString() ) );
		labelTable.put( (Integer)slider_max, new JLabel( ((Integer)slider_max).toString() ) );
		myTrailWidthSlider.setLabelTable(labelTable);
		myTrailWidthSlider.setPaintLabels(true);
		
		myTrailWidthSlider.addChangeListener(
			new MethodAction(myController, 
			"setTrailWidth", myModel, myTrailWidthSlider)		
		);

		panel.add(new JLabel("Speed"));
		panel.add(myTrailWidthSlider);
		add(panel);		
	}

	private void createStrokeChooser() {
		JPanel panel = new JPanel();
		panel.add(new JLabel("Stroke"));
		myTrailStroke = new JComboBox((Object[])findStrokeOptions());
		myTrailStroke.setEditable(false);		

		myTrailStroke.addActionListener(
			new MethodAction(myController, 
			"setStroke", myModel, myTrailStroke)		
		);

		panel.add(myTrailStroke);
		add(panel);
	}

	private String getCurrentStroke() {
		return (String) myTrailStroke.getSelectedItem();
	} 

	private String[] findStrokeOptions() {
		String[] patternExamples = {
                "stroke 1",
                "stroke 2",
                "stroke 3"
                };
		
		return patternExamples;
	}*/

	private void createTurtleChooser() {
		JPanel panel = new JPanel();
		panel.add(new JLabel("Turtle"));
		String[] currentTurtles = myController.getTurtles(myModel);

		myTurtlesCombo = new JComboBox(currentTurtles);
		selectedTurtle = (String) myTurtlesCombo.getSelectedItem();
		
		myTurtlesCombo.setEditable(false);
		myTurtlesCombo.addActionListener(createActionListener());
		panel.add(myTurtlesCombo);
		add(panel);
	}

	private ActionListener createActionListener() {
		listener = new ActionListener() {					 
            public void actionPerformed(ActionEvent e)
            {
            	//selectedTurtle = (String) myTurtlesCombo.getSelectedItem();
            	myController.setSelectedTurtle(myTurtlesCombo.getSelectedIndex());
            	System.out.println("Current turtle is: " + selectedTurtle);
            	//get turtle info from model
            	//update all sections related to turtle
            	for(AbstractGroup group : linkedObjects) {			
        			group.update();
        		}
            	System.out.println("turtles updated!asdfasdf");
            }
		};
		return listener;
	}

	public void update(Observable o, Object arg) {
		System.out.println("starting update!");
		myTurtlesCombo.removeActionListener(listener);
		myTurtlesCombo.removeAllItems();
		ArrayList<Turtle> turtles = (ArrayList<Turtle>) myModel.getAllTurtles();
//		String[] currentTurtles = myController.getTurtles(myModel);
//		for(String s : currentTurtles) 
//			myTurtlesCombo.addItem(s);
		for(int i = 0; i < turtles.size(); i++) {
			myTurtlesCombo.addItem(i);
		}
		myController.setSelectedTurtle(myTurtlesCombo.getSelectedIndex());
		System.out.println("Current turtle is before update: " + myTurtlesCombo.getSelectedIndex());
		for(AbstractGroup group : linkedObjects) {	
			group.update();
		}
		myTurtlesCombo.addActionListener(listener);
		System.out.println("turtles updated!asdf");
	}
	
	public static void main(String args[]) throws IOException {
		Model model = new Model();
		PropertiesController controller = new PropertiesController(model);
		TurtlePropertiesPanel p = new TurtlePropertiesPanel("src/resources/properties/TurtleProperties.txt", model, controller);
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//f.setLayout(new FlowLayout());
		f.add(p);
		System.out.println("pre-starting update!");
		p.update(null, null);
		f.setSize(1000,500);
		f.setVisible(true);
	}


}
